<?php

namespace App\Kuafu\Banner\Controller\B;

use App\Kuafu\Banner\Enum\BannerCodeEnum;
use App\Kuafu\Banner\Exceptions\BannerException;
use App\Kuafu\Banner\Service\BannerCommonService;
use App\Kuafu\Banner\Validate\BannerCommonValidate;
use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * @author shanchengqingye
 * @description banner控制器
 *
 * @create-time 2022-02-09 19:51:55
 */
class BannerCommonController extends BaseController
{
    /**
     * 获取banner配置
     *
     * @return JsonResponse
     * @throws Exception
     */
    public function bannerConfig(): JsonResponse
    {
        return $this->success(BannerCommonService::bannerConfig());
    }

    /**
     * 添加banner
     *
     * @param Request $request
     * @return JsonResponse
     * @throws BannerException
     */
    public function insert(Request $request): JsonResponse
    {
        list($errMsg, $bannerParams, $bannerShopParams) = BannerCommonValidate::bannerInsertValidate($request->all());
        if ($errMsg) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_PARAM_VALID, $errMsg);
        }

        BannerCommonService::insert(
            $request->header('brandId'),
            auth('api')->user()->id,
            $bannerParams,
            $bannerShopParams,
        );
        return $this->success([]);
    }


    /**
     * 编辑banner
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws BannerException
     */
    public function edit(Request $request, int $id): JsonResponse
    {
        list($errMsg, $bannerParams, $bannerShopParams) = BannerCommonValidate::bannerEditValidate(
            $request->merge(['id' => $id])->all()
        );
        if ($errMsg) {
            return $this->fail(BannerCodeEnum::ERR_BANNER_PARAM_VALID, $errMsg);
        }

        BannerCommonService::edit(
            $request->header('brandId'),
            auth('api')->user()->id,
            $bannerParams,
            $bannerShopParams,
        );
        return $this->success([]);
    }

    /**
     * 删除banner
     *
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws BannerException
     */
    public function del(Request $request, $id): JsonResponse
    {
        list($errMsg, $requestParams) = BannerCommonValidate::delValidate($request->merge(['id' => $id])->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        BannerCommonService::del(auth()->id(), $requestParams);
        BannerCommonService::sendBannerConfigUpdateMQTT((int)$requestParams['id']);

        return $this->success();
    }

    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $shopId = (int)$request->get('shopId', 0);
        $pageSize = (int)$request->get('pageSize', 20);
        $pageNum  = (int)$request->get('pageNum', 1);
        $name = $request->get('name', '');
        $type = $request->get('type', '');
        $status = $request->get('status', '');
        return $this->success(BannerCommonService::index($pageNum, $pageSize, $name, $type, $status, $shopId));
    }

    /**
     * 禁用｜启用
     *
     * @param Request $request
     * @return JsonResponse
     * @throws BannerException
     */
    public function status(Request $request): JsonResponse
    {
        list($errMsg, $requestParams) = BannerCommonValidate::statusValidate($request->all());
        if ($errMsg) {
            return $this->fail(BaseCodeEnum::ERR_400000, $errMsg);
        }
        BannerCommonService::status(auth()->id(), $requestParams);
        BannerCommonService::sendBannerConfigUpdateMQTT((int)$requestParams['id']);

        return $this->success();
    }

    /**
     * 单品素材搜索
     * @param Request $request
     * @return JsonResponse
     */
    public function bannerSearch(Request $request): JsonResponse
    {
        $name = $request->get('name', '');
        return $this->success(BannerCommonService::bannerSearch($name));
    }

    /**
     * 删除提示,获取关联多少个素材
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function getCount(Request $request, $id): JsonResponse
    {
        return $this->success(BannerCommonService::getCount($id));
    }
}
